Ext.define('GBIF.OverviewPanel', {
		extend: 'Ext.panel.Panel'	
	,	alias: 'widget.overviewPanel'
	,	layout: 'fit'
	,	border: false
	,	autoScroll: true
	,	newCalled: false
	,	classifyCalled: false
	,	archiveCalled: false
	,	tpl: Config.overviewTpl
	,	isTbarVisible: false
	,	defaultInterval: 5000
	,	initComponent: function() {
			this.task = {
					run: this.getStatusRequest
				,	interval: this.defaultInterval
				,	scope: this
			}
			this.runner = new Ext.util.TaskRunner();
			this.callParent();
		}
	,	showHideTbar: function(status){
			if(status == "CLASSIFYING"){
				this.showTbar();
			} else {
				this.hideTbar();
			}
		}	
	,	showTbar: function(){
			if(!this.isTbarVisible){
				this.statusProgrss = Ext.create('Ext.ProgressBar', {
					width: 300
				});
				this.progressTbar = this.addDocked({
					xtype: 'toolbar',
					dock: 'top',
					items: [
					'&nbsp;'
				,	{
						xtype: 'label'
					,	text: 'Progress: '
					}
				,	'&nbsp;'
				,	 this.statusProgrss]
				});
				this.isTbarVisible = true;
			}
		}	
	,	hideTbar: function(){
			if(this.isTbarVisible){
				if(Ext.isDefined(this.progressTbar[0])){
					this.removeDocked(this.progressTbar[0]);
				}
				this.isTbarVisible = false;
			}
		}
	,	getStatusRequest: function(){
			Ext.Ajax.request({
					url: Config.baseUrl + 'resources/api/api.php'
				,	scope: this
				,	params: { 
							cmd: 'getStatus'
						, 	projectId: GBIF.currentProjectID
					}
				,	success: function(r) {
						var o = Ext.decode(r.responseText);
						if (o.success) {
							this.showHideTbar(o.status);
							if(o.status == "CLASSIFYING" && Ext.isDefined(this.statusProgrss)){
									this.statusProgrss.updateProgress(Ext.util.Format.round(o.x/o.y), o.x + '/' + o.y ); 
							}
							if(o.status == "ARCHIVED"){
								GBIF.currentArchiveUrl = o.archiveUrl
								this.updateTpl(o.status);
							} else if(o.status == "ERROR"){
								GBIF.currentErrorCode = o.comments
								this.updateTpl(o.status);
							} else {
								GBIF.currentErrorCode = ''
								GBIF.currentArchiveUrl = '';
								this.updateTpl(o.status);
							}
						} else {
								GBIF.Notice.msg(this.msgErrorTitle, this.msgStatusError);
						}
					}
				,	failure: function() {
						GBIF.Notice.msg(this.msgErrorTitle, this.msgFatalError);
					}
			});
		}
	,	updateData: function(){		
			var data = {
					status: GBIF.currentStatus
				,	pid: GBIF.currentProjectID
				,	lang: lang
			}
			if(GBIF.currentStatus == "ARCHIVED"){
				data.archiveUrl = GBIF.currentArchiveUrl;
			}
			if(GBIF.currentStatus == "ERROR"){
				data.comments = GBIF.currentErrorCode;
			}
			this.update(data);
		}
	,	updateTpl: function(status){
			switch(status){
				case "NEW":
					if(!this.newCalled){
						this.startTimer();
						this.newCalled = true;
						this.updateData();
					}
					break;
				case "CLASSIFYING":
					if(!this.classifyCalled){
						this.task.interval = 6000;
						this.startTimer();
						this.newCalled = false;
						this.classifyCalled = true;
						this.updateData();
					}
					break;
				case "ARCHIVING":
					if(!this.archiveCalled){
						this.startTimer();
						this.newCalled = false;
						this.classifyCalled = false;
						this.archiveCalled = true;
						this.updateData();
						this.fireEvent('archivingStatus', this);
					}
					break;
				default:
					GBIF.currentStatus = status
					this.updateData();
					this.fireEvent('statusChanged', this);
					this.stopTimer();
					this.classifyCalled = false;
					this.newCalled = false;
					break;
			}
		}
	,	resetAll: function(){
			this.classifyCalled = false;
			this.newCalled = false;
			this.archiveCalled = false;
			this.stopTimer();
		}	
	,	startTimer: function(){
			this.runner.stopAll();
			this.runner.start(this.task);
		}
	,	stopTimer: function(){
			this.runner.stop(this.task);
			this.task.interval = this.defaultInterval;
		}
});